import Vue from "vue";
import VueRouter from "vue-router";
import axios from "@/api/request.js";
import Login from "@/views/Login/Login.vue";
import Home from "@/views/Home/Home.vue";
import Register from "@/views/Register/Register.vue";
import RegisterSuccess from "@/views/Register/success/success.vue";
import childrenRouter from "@/router/childrenRouter";
import childrenRouterSS2 from "@/router/childrenRouterSS2";
Vue.use(VueRouter);

//获取动态组件的方法
// const _import = require("./_import");
const children = localStorage.getItem('SYS_TYPE') === 'SS1' ? childrenRouter : childrenRouterSS2
export const constantRoutes = [{
	path: "/",
	name: "Home",
	component: Home,
	hidden: true,
	redirect: () => ({
		name: "Login"
	}),
	children: children
},
{
	path: "/idaas",
	name: "iDaaS",
	component: () => import("@/views/iDaaS/index.vue"),
	hidden: true,
	meta: {
		auth: true,
		title: "idaas-渔业资源系统登录",
		name: "idaas-渔业资源系统登录",
	},
},

{
	path: "/idaasPlant",
	name: "iDaasPlant",
	component: () => import("@/views/iDaaS/index.vue"),
	hidden: true,
	meta: {
		auth: true,
		title: "idaas-种植资源系统登录",
		name: "idaas-种植资源系统登录",
	},
},
{
	path: "/Login",
	name: "Login",
	component: Login,
	hidden: true,
	meta: {
		auth: true,
		title: "渔业资源管理系统-登录",
		name: "渔业资源管理系统-登录",
	},
},
{
	path: "/Landed",
	name: "Landed",
	component: Login,
	hidden: true,
	meta: {
		auth: true,
		title: "种植资源管理系统-登录",
		name: "种植资源管理系统-登录",
	},
},
{
	path: "/Register",
	name: "Register",
	component: Register,
	hidden: true
},
{
	path: "/RegisterSuccess",
	name: "RegisterSuccess",
	component: RegisterSuccess,
	hidden: true,
	meta: {
		auth: false,
		title: "注册成功",
		name: "注册成功",
	},
},
{
	path: "/404",
	component: () => import("@/views/404/404.vue"),
	hidden: true,
},
{
	path: "*",
	redirect: "/404",
	hidden: true
},
];




// 在使用ElementUi时点击同一个路由，页面报错
// 解决方法如下
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err);
};



const createRouter = () =>
	new VueRouter({
		scrollBehavior(to) {
			if (to.hash) {
				return {
					selector: to.hash,
					behavior: "smooth",
				};
			}
		},
		mode: "hash",
		base: process.env.BASE_URL,
		routes: constantRoutes,
	});

const router = createRouter();

export function resetRouter() {
	const newRouter = createRouter();
	router.matcher = newRouter.matcher;
}

// /**
//  * 递归动态权限路由表
//  * 并且创建动态权限路由表
//  */
// let forRouter = function (parentPath = "/", dynamicRoute) {
// 	dynamicRoute.forEach((item) => {
// 		if (item.children && item.children.length > 0) {
// 			forRouter(parentPath, item.children);
// 		}
// 		item.component = _import(item.component);
// 		let parentRouterPath = item.path==='/Layout'?'/Layout':`${parentPath}/${item.path}`
// 		router.addRoute(parentRouterPath, item);
// 		console.log(parentRouterPath,item);
// 	});

// 	return dynamicRoute;
// };


// let asyncGetRouter = function (msg = '启用动态路由加载') {
// 	console.group(msg)
// 	return axios.get("/router/index.json").then((res) => {
//         store.dispatch("asyncGetRouter/setAsyncAddRoute", res)
// 		.then((dynamicRoute)=>{
// 			forRouter("/Layout", dynamicRoute)
// 		})
// 		return Promise.resolve(res)
//     });
// };
// asyncGetRouter();
// console.log(JSON.stringify([
// 	Map,
// 	TaskPlann,
// 	DataResource,
// 	NamedManagement,
// 	BasicInfo,
// 	System,
// 	Help,
// ]));


// let url = "/uaa/user/info"

// router.beforeEach((to, from, next) => {
// 	// let user = store.state.User.userInfo;
// 	// if (to.name === "Login") {
// 	// 	next();
// 	// } else {
// 	// 	if (!user.token) {
// 	// 		next({
// 	// 			name: "Login"
// 	// 		});
// 	// 	}
// 	// }
// 	next();
// });

router.beforeEach((to, from, next) => {


	if (!localStorage.getItem('getTreeData/setAsyncVillageData')) {
		localStorage.setItem('getTreeData/setAsyncVillageData', '[{"label":"佛山市","value":"440600","children":[{"label":"南海区","value":"440605","children":[{"label":"桂城街道","value":"440605011","children":[{"label":"桂一村","value":"440605011001"},{"label":"桂二村","value":"440605011002"},{"label":"桂三村","value":"440605011003"},{"label":"叠二村","value":"440605011004"},{"label":"花苑村","value":"440605011005"},{"label":"育才村","value":"440605011006"},{"label":"桂雅村","value":"440605011007"},{"label":"桂圆村","value":"440605011008"},{"label":"南桂村","value":"440605011009"},{"label":"大圩村","value":"440605011010"},{"label":"江滨村","value":"440605011011"},{"label":"大德村","value":"440605011012"},{"label":"东平村","value":"440605011013"},{"label":"怡翠玫瑰村","value":"440605011014"},{"label":"江南名居村","value":"440605011015"},{"label":"翠颐村","value":"440605011016"},{"label":"万馨村","value":"440605011017"},{"label":"富景村","value":"440605011018"},{"label":"海逸村","value":"440605011019"},{"label":"灯湖村","value":"440605011020"},{"label":"东约村","value":"440605011021"},{"label":"南约村","value":"440605011022"},{"label":"西约村","value":"440605011023"},{"label":"北约村","value":"440605011024"},{"label":"东二村","value":"440605011025"},{"label":"石肯村","value":"440605011026"},{"label":"叠南村","value":"440605011027"},{"label":"叠北村","value":"440605011028"},{"label":"平东村","value":"440605011029"},{"label":"平南村","value":"440605011030"},{"label":"平西村","value":"440605011031"},{"label":"平北村","value":"440605011032"},{"label":"平胜村","value":"440605011033"},{"label":"夏东村","value":"440605011034"},{"label":"夏南一村","value":"440605011035"},{"label":"夏南二村","value":"440605011036"},{"label":"夏北村","value":"440605011037"},{"label":"中区村","value":"440605011038"},{"label":"东区村","value":"440605011039"},{"label":"北区村","value":"440605011040"},{"label":"林岳村","value":"440605011041"},{"label":"夏西村","value":"440605011217"}]},{"label":"九江镇","value":"440605121","children":[{"label":"江滨村","value":"440605121001"},{"label":"儒林村","value":"440605121002"},{"label":"沙口村","value":"440605121003"},{"label":"水南村","value":"440605121004"},{"label":"下西村","value":"440605121006"},{"label":"南方村","value":"440605121007"},{"label":"朗星村","value":"440605121008"},{"label":"沙咀村","value":"440605121010"},{"label":"敦根村","value":"440605121011"},{"label":"梅圳村","value":"440605121012"},{"label":"大谷村","value":"440605121013"},{"label":"河清村","value":"440605121014"},{"label":"下北村","value":"440605121015"},{"label":"上东村","value":"440605121016"},{"label":"英明村","value":"440605121017"},{"label":"沙头村","value":"440605121018"},{"label":"璜矶村","value":"440605121019"},{"label":"石江村","value":"440605121035"},{"label":"北村村","value":"440605121037"},{"label":"新龙村","value":"440605121041"},{"label":"上西村","value":"440605121201"},{"label":"海寿村","value":"440605121206"},{"label":"下东村","value":"440605121211"},{"label":"烟南村","value":"440605121214"},{"label":"镇南村","value":"440605121216"},{"label":"南金村","value":"440605121222"},{"label":"物流产业园九江片一区","value":"440605121401"},{"label":"物流产业园九江片三区","value":"440605121403"},{"label":"物流产业园沙头片一区","value":"440605121404"},{"label":"物流产业园沙头片二区","value":"440605121405"},{"label":"西桥村","value":"440605121042"}]},{"label":"西樵镇","value":"440605122","children":[{"label":"东碧村","value":"440605122001"},{"label":"樵华村","value":"440605122002"},{"label":"樵园村","value":"440605122003"},{"label":"樵乐村","value":"440605122005"},{"label":"西樵村","value":"440605122006"},{"label":"联新村","value":"440605122007"},{"label":"山根村","value":"440605122008"},{"label":"西岸村","value":"440605122010"},{"label":"显岗村","value":"440605122011"},{"label":"民乐村","value":"440605122012"},{"label":"百西村","value":"440605122013"},{"label":"崇南村","value":"440605122015"},{"label":"太平村","value":"440605122016"},{"label":"上金欧村","value":"440605122030"},{"label":"崇北村","value":"440605122033"},{"label":"河岗村","value":"440605122035"},{"label":"新河村","value":"440605122037"},{"label":"稔岗村","value":"440605122041"},{"label":"朝山村","value":"440605122043"},{"label":"大同村","value":"440605122045"},{"label":"樵泰村","value":"440605122047"},{"label":"爱国村","value":"440605122048"},{"label":"简村村","value":"440605122049"},{"label":"岭西村","value":"440605122201"},{"label":"儒溪村","value":"440605122206"},{"label":"百东村","value":"440605122208"},{"label":"华夏村","value":"440605122211"},{"label":"大岸村","value":"440605122212"},{"label":"平沙村","value":"440605122218"},{"label":"海舟村","value":"440605122219"},{"label":"新田村","value":"440605122220"},{"label":"七星村","value":"440605122224"},{"label":"西樵科技工业园村","value":"440605122401"},{"label":"轻纺城村","value":"440605122402"}]},{"label":"丹灶镇","value":"440605123","children":[{"label":"丹灶村","value":"440605123001"},{"label":"南沙村","value":"440605123005"},{"label":"石联村","value":"440605123007"},{"label":"联沙村","value":"440605123008"},{"label":"上安村","value":"440605123009"},{"label":"仙岗村","value":"440605123016"},{"label":"西城村","value":"440605123017"},{"label":"银河村","value":"440605123018"},{"label":"东升村","value":"440605123019"},{"label":"劳边村","value":"440605123020"},{"label":"下沙滘村","value":"440605123021"},{"label":"云溪村","value":"440605123022"},{"label":"金宁村","value":"440605123023"},{"label":"建设村","value":"440605123024"},{"label":"沙滘村","value":"440605123026"},{"label":"罗行村","value":"440605123028"},{"label":"东联村","value":"440605123038"},{"label":"西联村","value":"440605123039"},{"label":"仙湖村","value":"440605123040"},{"label":"新安村","value":"440605123041"},{"label":"高海村","value":"440605123042"},{"label":"新农村","value":"440605123043"},{"label":"荷村村","value":"440605123044"},{"label":"良登村","value":"440605123211"},{"label":"塱心村","value":"440605123212"},{"label":"下滘村","value":"440605123224"},{"label":"中安村","value":"440605123225"},{"label":"西岸村","value":"440605123226"},{"label":"下安村","value":"440605123227"},{"label":"华南环保产业园村","value":"440605123401"},{"label":"五金产业基地村","value":"440605123402"}]},{"label":"狮山镇","value":"440605124","children":[{"label":"狮城村","value":"440605124001"},{"label":"塘中村","value":"440605124004"},{"label":"唐边村","value":"440605124007"},{"label":"联表村","value":"440605124013"},{"label":"山南村","value":"440605124014"},{"label":"石碣村","value":"440605124015"},{"label":"小塘村","value":"440605124017"},{"label":"华涌村","value":"440605124027"},{"label":"穆园村","value":"440605124029"},{"label":"塘头村","value":"440605124030"},{"label":"白沙桥村","value":"440605124031"},{"label":"招大村","value":"440605124032"},{"label":"洞边村","value":"440605124038"},{"label":"五星村","value":"440605124041"},{"label":"群岗村","value":"440605124045"},{"label":"永安村","value":"440605124050"},{"label":"大榄村","value":"440605124053"},{"label":"红星村","value":"440605124055"},{"label":"丁圃村","value":"440605124056"},{"label":"沙头村","value":"440605124057"},{"label":"高边村","value":"440605124060"},{"label":"兴贤村","value":"440605124061"},{"label":"颜峰村","value":"440605124062"},{"label":"谭边村","value":"440605124063"},{"label":"横岗村","value":"440605124064"},{"label":"罗湖村","value":"440605124065"},{"label":"罗湖北村","value":"440605124066"},{"label":"联和村","value":"440605124068"},{"label":"街边村","value":"440605124069"},{"label":"沙坑村","value":"440605124070"},{"label":"上柏村","value":"440605124071"},{"label":"下柏村","value":"440605124072"},{"label":"罗村村","value":"440605124073"},{"label":"务庄村","value":"440605124074"},{"label":"朗沙村","value":"440605124075"},{"label":"芦塘村","value":"440605124076"},{"label":"显纲村","value":"440605124080"},{"label":"联星村","value":"440605124090"},{"label":"官窑城区","value":"440605124091"},{"label":"乐安村","value":"440605124092"},{"label":"七甫村","value":"440605124094"},{"label":"驿园村","value":"440605124095"},{"label":"桃园村","value":"440605124096"},{"label":"大学城村","value":"440605124097"},{"label":"唐边村","value":"440605124201"},{"label":"黄洞村","value":"440605124202"},{"label":"狮中村","value":"440605124203"},{"label":"狮北村","value":"440605124204"},{"label":"罗洞村","value":"440605124205"},{"label":"塱下村","value":"440605124206"},{"label":"大涡塘村","value":"440605124208"},{"label":"狮西村","value":"440605124213"},{"label":"狮岭村","value":"440605124214"},{"label":"狮南村","value":"440605124215"},{"label":"莲子塘村","value":"440605124216"},{"label":"莲塘村","value":"440605124219"},{"label":"新境村","value":"440605124220"},{"label":"象岭村","value":"440605124226"},{"label":"南浦村","value":"440605124227"},{"label":"石澎村","value":"440605124228"},{"label":"黎岗村","value":"440605124229"},{"label":"吴屋村","value":"440605124231"},{"label":"永和村","value":"440605124232"},{"label":"刘边村","value":"440605124234"},{"label":"新和村","value":"440605124238"},{"label":"凤岗村","value":"440605124239"},{"label":"塘联村","value":"440605124244"},{"label":"万石村","value":"440605124245"},{"label":"显子岗村","value":"440605124246"},{"label":"龙头村","value":"440605124247"},{"label":"沙水村","value":"440605124248"},{"label":"石泉村","value":"440605124249"},{"label":"南海科技工业园松夏一区村","value":"440605124401"},{"label":"南海科技工业园松夏三区村","value":"440605124402"},{"label":"南海科技工业园南区村","value":"440605124403"},{"label":"南海科技工业园北区村","value":"440605124404"},{"label":"南海科技工业园核心区村","value":"440605124405"},{"label":"三环西汽配园区村","value":"440605124406"},{"label":"长虹岭工业园村","value":"440605124410"},{"label":"沙堤机场","value":"440605124411"}]},{"label":"大沥镇","value":"440605125","children":[{"label":"沥苑村","value":"440605125001"},{"label":"沥南村","value":"440605125002"},{"label":"沥雄村","value":"440605125003"},{"label":"沥兴村","value":"440605125004"},{"label":"直街村","value":"440605125005"},{"label":"六村村","value":"440605125006"},{"label":"华夏村","value":"440605125007"},{"label":"岐阳村","value":"440605125008"},{"label":"嘉怡村","value":"440605125010"},{"label":"洞庭村","value":"440605125011"},{"label":"江北村","value":"440605125012"},{"label":"沥北村","value":"440605125016"},{"label":"沥东村","value":"440605125017"},{"label":"沥西村","value":"440605125018"},{"label":"沥中村","value":"440605125019"},{"label":"雅瑶村","value":"440605125020"},{"label":"联滘村","value":"440605125021"},{"label":"凤池村","value":"440605125022"},{"label":"水头村","value":"440605125023"},{"label":"奇槎村","value":"440605125024"},{"label":"钟边村","value":"440605125025"},{"label":"大镇村","value":"440605125026"},{"label":"谢边村","value":"440605125027"},{"label":"曹边村","value":"440605125028"},{"label":"太平村","value":"440605125029"},{"label":"黄岐城区","value":"440605125035"},{"label":"六联村","value":"440605125036"},{"label":"泌冲村","value":"440605125038"},{"label":"白沙村","value":"440605125039"},{"label":"沙溪村","value":"440605125040"},{"label":"盐步城区","value":"440605125041"},{"label":"河东村","value":"440605125042"},{"label":"河西村","value":"440605125043"},{"label":"联安村","value":"440605125044"},{"label":"平地村","value":"440605125045"},{"label":"横江村","value":"440605125046"},{"label":"东秀村","value":"440605125047"},{"label":"雍雅村","value":"440605125049"},{"label":"沥雅村","value":"440605125050"},{"label":"沥桂村","value":"440605125051"},{"label":"岐城村","value":"440605125052"},{"label":"渔业村","value":"440605125053"}]},{"label":"里水镇","value":"440605126","children":[{"label":"沙涌村","value":"440605126004"},{"label":"草场村","value":"440605126005"},{"label":"河村村","value":"440605126006"},{"label":"甘蕉村","value":"440605126007"},{"label":"胜利村","value":"440605126008"},{"label":"大步村","value":"440605126009"},{"label":"洲村村","value":"440605126010"},{"label":"大冲村","value":"440605126011"},{"label":"大石村","value":"440605126012"},{"label":"邓岗村","value":"440605126014"},{"label":"和顺城区","value":"440605126015"},{"label":"里水村","value":"440605126017"},{"label":"金溪村","value":"440605126018"},{"label":"流潮村","value":"440605126023"},{"label":"共同村","value":"440605126044"},{"label":"白岗村","value":"440605126045"},{"label":"文教村","value":"440605126046"},{"label":"金利村","value":"440605126047"},{"label":"新兴村","value":"440605126048"},{"label":"新联村","value":"440605126049"},{"label":"金峰洲村","value":"440605126050"},{"label":"金旗峰村","value":"440605126051"},{"label":"得胜村","value":"440605126209"},{"label":"宏岗村","value":"440605126210"},{"label":"赤山村","value":"440605126211"},{"label":"麻奢村","value":"440605126212"},{"label":"布新村","value":"440605126213"},{"label":"岗联村","value":"440605126214"},{"label":"北沙村","value":"440605126216"},{"label":"瑶头村","value":"440605126221"},{"label":"鹤峰村","value":"440605126222"},{"label":"贤僚村","value":"440605126227"},{"label":"鲁岗村","value":"440605126228"},{"label":"汤村村","value":"440605126229"},{"label":"小埗村","value":"440605126230"},{"label":"建星村","value":"440605126232"},{"label":"逢涌村","value":"440605126233"},{"label":"石塘村","value":"440605126234"},{"label":"和桂工业园村","value":"440605126401"}]}]}]}]')
	}


	if (to.meta.title) document.title = to.meta.title;
	let outLogin = {}
	if (to.name === "Login" || to.name === "iDaaS") {
		localStorage.setItem('SYS_TYPE', 'SS1')
		outLogin = {
			name: "Login"
		}
	} else if (to.name === "Landed"  || to.name === "iDaasPlant") {
		localStorage.setItem('SYS_TYPE', 'SS2')
		outLogin = {
			name: "Landed"
		}
	} else {
		outLogin = JSON.parse(localStorage.getItem('outLogin')) || {
			name: "Login"
		}
	}
	localStorage.setItem('outLogin', JSON.stringify(outLogin))
	if (to.name === "Login" || to.name === 'Register' || to.name === 'Landed' || to.name === 'iDaaS' || to.name === 'iDaasPlant' || to.name === 'RegisterSuccess') {
		next();
	} else {
		if (to.matched.some(record => record.meta.auth)) {
			axios.get('/uaa/user/info').then((res) => {
				let userInfo = {}
				if (localStorage.getItem('userInfo')) {
					userInfo = JSON.parse(localStorage.getItem('userInfo'))
				}
				if (res.authorities instanceof Array) {
					if (to.meta.roles.includes(userInfo.userLevel)) {
						next()
					} else {
						next(outLogin) // 确保一定要调用 next()
					}
				} else {
					next(outLogin) // 确保一定要调用 next()
				}
			})
		} else {
			next(outLogin) // 确保一定要调用 next()
		}
	}


})
// console.log(JSON.stringify(router.options.routes[0].children));
// console.log(router.options.routes[0].children);
export default router;
